﻿#include "FITKFESubSysStructure.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsFEModel.h"
#include "FITKAcousticsFEFace.h"
#include "FITKAcousticsTriangles.h"
#include "FITKAcousticsFENodeMap.h"
#include "FITKAcousticsDiscNodeList.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractElement.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementFactory.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementFactory.h"
#include "FITK_Component/FITK2DMeshTools/FITKAbstractSeparatePlate.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementTri.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include <QSet>

namespace Acoustics
{

    FITKFESubSysStructure::FITKFESubSysStructure()
    {
    }

    FITKFESubSysStructure::~FITKFESubSysStructure()
    {
    }

    FITKAbstractFESubSys::FESubSysType FITKFESubSysStructure::getFESubSysType()
    {
        //获取子系统类型
        return FITKAbstractFESubSys::FESubSysType::FEStructure;
    }

    FITKAcousticsFEFace* FITKFESubSysStructure::generateFEFace()
    {
        if (_elementsIDList.isEmpty()) return nullptr;
        //获取FE模型数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return nullptr;
        FITKAcousticsFEModel* feModel = discModel->getFEModel();
        if (!feModel) return nullptr;
        //获取单元列表
        FITKAcousticsFESubSysElementList* elementList = feModel->getFESubSysElementList();
        if (!elementList) return nullptr;
        //创建新的单元数据
        QList<Interface::FITKAbstractElement*> elemList;
        for (int eleID : _elementsIDList)
        {
            //获取单元数据
            Interface::FITKAbstractElement* elemData = elementList->getElementByID(eleID);
            if (!elemData) continue;
            int nodeCount = elemData->getNodeCount();
            //创建三角形数据
            if (elemData->getEleType() == Interface::FITKModelEnum::FITKEleType::Tri3 && nodeCount == 3)
            {
                Interface::FITKAbstractElement* element = Interface::FITKElementFactory::createElement(Interface::FITKModelEnum::FITKEleType::Tri3);
                element->setNodeID(elemData->getAllNodes());
                elemList.append(element);
            }
            else if (elemData->getEleType() == Interface::FITKModelEnum::FITKEleType::Quad4 && nodeCount == 4)
            {
                Interface::FITKAbstractElement* element = Interface::FITKElementFactory::createElement(Interface::FITKModelEnum::FITKEleType::Tri3);
                element->setNodeID(QList<int>() << elemData->getNodeID(0) << elemData->getNodeID(1) << elemData->getNodeID(3));
                elemList.append(element);
                element = Interface::FITKElementFactory::createElement(Interface::FITKModelEnum::FITKEleType::Tri3);
                element->setNodeID(QList<int>() << elemData->getNodeID(1) << elemData->getNodeID(2) << elemData->getNodeID(3));
                elemList.append(element);
            }
        }
        if (elemList.isEmpty()) return nullptr;
        //创建FE面
        FITKAcousticsFEFace* feFace = new FITKAcousticsFEFace;
        FITKAcousticsTriangles* mesh = feFace->getTriangleMesh();
        if (!mesh)
        {
            for (auto ele : elemList)
                if (ele) delete ele;
            delete feFace;
            return nullptr;
        }
        for (auto ele : elemList)
            mesh->appendElement(ele);
        return feFace;
    }



    void FITKFESubSysStructure::setDampingLossSpectraID(int dampSpecID)
    {
        //设置阻尼谱损失ID
        _dampLossSpectraID = dampSpecID;
    }

    int FITKFESubSysStructure::getDampingLossSpectraID()
    {
        //获取阻尼损失谱ID
        return _dampLossSpectraID;
    }

    FITKAcousticsAbstractSpectra* FITKFESubSysStructure::getDampingLossSpectraObj()
    {
        //获取阻尼损失谱数据对象
        return FITKDATAREPO->getTDataByID<FITKAcousticsAbstractSpectra>(_dampLossSpectraID);
    }
}


